//
//  JXLBaseViewController.m
//  piggy
//
//  Created by 宋质彬 on 2018/3/5.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JXLBaseViewController.h"

@interface JXLBaseViewController ()
@property (strong, nonatomic) MBProgressHUD             *progressHud;
@end

@implementation JXLBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    _progressHud = [[MBProgressHUD alloc] initWithView:self.view];
    self.progressHud.bezelView.alpha = 0.5;
//    self.view.e
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置提示
- (void)showMessage:(NSString *)msg {
    if ([msg isEqualToString:@""]) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.offset = CGPointMake(hud.offset.x, hud.offset.y - 50);
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = REGULAR_FONT(17);
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:0.75];
}

- (void)showLoading:(NSString *)msg{
    //    if (msg && msg.length > 0) {
    //
    //    }else{
    //        msg = [TipMessage shared].tipMessage.tipRequestSending;
    //    }
    self.progressHud.frame = self.view.bounds;
    self.progressHud.offset = CGPointMake(self.progressHud.offset.x, self.progressHud.offset.y - 50);
    [self.view addSubview:self.progressHud];
    self.progressHud.label.text = msg;
    self.progressHud.removeFromSuperViewOnHide = YES;
    [self.progressHud showAnimated:YES];
}

- (void)hideLoading{
    [self.progressHud hideAnimated:YES];
}
#pragma --mark 页面基本设置
/**
 *  页面基本设置,页面背景颜色、页面是否包含返回按钮、对返回按钮做相应的处理
 *
 *  @param gobackType 页面返回类型
 */
- (void)baseConfig:(PageGobackType)gobackType{
    
    //设置页面背景
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    //设置返回按钮
    if (gobackType != PageGobackTypeNone) {
        UIButton *btnLeft = [[UIButton alloc] init];
        btnLeft.frame = CGRectMake(0, 0, 44, 44);
        [btnLeft setTitle:@"" forState:UIControlStateNormal];
        [btnLeft setImage:[UIImage imageNamed:@"app_go_back"] forState:UIControlStateNormal];
        
        if (gobackType == PageGobackTypePop) {
            [btnLeft addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeDismiss){
            [btnLeft addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeRoot){
            [btnLeft addTarget:self action:@selector(popToRootViewController) forControlEvents:UIControlEventTouchUpInside];
        }
        
    
        btnLeft.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        [btnLeft setImageEdgeInsets:UIEdgeInsetsMake(0,0,0, -5 *AutoSizeScaleXX)];
    
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
        
        
        //        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
        //                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
        //                                           target:nil action:nil];
        //        negativeSpacer.width = -25;
        [self.navigationItem setLeftBarButtonItems:@[leftItem]];
    }else{
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
        [self.navigationItem setLeftBarButtonItem:leftItem];
    }
}

/**
 *  页面基本设置,页面背景颜色、页面是否包含返回按钮、对返回按钮做相应的处理
 *
 *  @param gobackType 页面返回类型
 */
- (void)baseConfigBackTitle:(PageGobackType)gobackType{
    
    //设置页面背景
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    //设置返回按钮
    if (gobackType != PageGobackTypeNone) {
        UIButton *btnLeft = [[UIButton alloc] init];
//        btnLeft.backgroundColor = [UIColor greenColor];
        btnLeft.frame = CGRectMake(0, 0, 24, 24);
        [btnLeft setTitle:@"" forState:UIControlStateNormal];
        [btnLeft setImage:[UIImage imageNamed:@"app_go_back"] forState:UIControlStateNormal];
//        [btnLeft sizeToFit];
        
        if (gobackType == PageGobackTypePop) {
            [btnLeft addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeDismiss){
            [btnLeft addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeRoot){
            [btnLeft addTarget:self action:@selector(popToRootViewController) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        btnLeft.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        [btnLeft setImageEdgeInsets:UIEdgeInsetsMake(0,-5 *AutoSizeScaleXX,0, 0)];
        
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
        
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setTitle:self.backTitle forState:UIControlStateNormal];
        [button1 setTitleColor:BACK_COLOR forState:UIControlStateNormal];
        button1.titleLabel.font = REGULAR_FONT(16);

        btnLeft.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

        [button1 setTitleEdgeInsets:UIEdgeInsetsMake(0,-20 *AutoSizeScaleXX,0, 0)];
        if (gobackType == PageGobackTypePop) {
            [button1 addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeDismiss){
            [button1 addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
        }else if (gobackType == PageGobackTypeRoot){
            [button1 addTarget:self action:@selector(popToRootViewController) forControlEvents:UIControlEventTouchUpInside];
        }
        [button1 sizeToFit];
        
        UIBarButtonItem *leftItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
        [self.navigationItem setLeftBarButtonItems:@[leftItem,leftItem1]];
    }else{
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
        [self.navigationItem setLeftBarButtonItem:leftItem];
    }
}

#pragma mark - 跳转

/**
 *  dimiss页面
 */
- (void)dismissViewController{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.tabBarController.tabBar.hidden = NO;
}

/**
 *返回任意一个页面
 */
- (void)popToViewControllerAtIndex:(NSInteger)index{
    if (self.navigationController.viewControllers.count > index) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index] animated:YES];
    }
}

/**
 *popToRoot页面
 */
- (void)popToRootViewController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/**
 *  pop页面
 */
- (void)popViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated{
    if (self.navigationController.viewControllers.count == 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [self.navigationController pushViewController:viewController animated:animated];
}

//- (UIStatusBarStyle)preferredStatusBarStyle {
//    //    return UIStatusBarStyleLightContent;
//    return UIStatusBarStyleLightContent;
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
